package lpg;

public class Main
{
    public static void main(String[] args)
    {
        //
        // If only "lpg.Main" or "lpg.Main ?*" or "lpg.Main -h" is typed, we display the help screen.
        //
        if (args.length == 0 || args[0].startsWith("?") || args[0].equals("-h"))
        {
            Option.printOptionsList();
            return;
        }

        if (args[0].equals("-version"))
        {
            System.out.println();
            System.out.println(Control.HEADER_INFO + " Version " + Control.VERSION);
            System.out.println();
            return;
        }

        Option option = null;
        try
        {
            option = new Option(args);
            LexStream lexStream = new LexStream(option);
            LookupTable<String, VariableSymbol> variableTable = new LookupTable<String, VariableSymbol>();
            LookupTable<String, MacroSymbol> macroTable = new LookupTable<String, MacroSymbol>();

            Scanner scanner = new Scanner(option, lexStream, variableTable, macroTable);
            scanner.scan();

            if (!option.quiet)
            {
                System.out.println();
                System.out.println(Control.HEADER_INFO + " Version " + Control.VERSION);
                System.out.println();
            }

            if (lexStream.numTokens() > 0 && scanner.numErrorTokens() == 0)
            {
                Control control = new Control(option, lexStream, variableTable, macroTable);
                control.processGrammar();
                control.constructParser();
            }

            option.close();
        }
        catch (LpgExitException e)
        {
        }
        catch (LpgException e)
        {
            System.out.println("***Error: " + e.getMessage());
        }
        finally
        {
            if (option != null)
                option.release();
        }
    }

}
